﻿#include "GuidedMissile.h"
#include"Game.h"
#include<qlabel.h>
GuidedMissile::GuidedMissile(int x, int y, QObject* parent)
	:Bullet(x,y)
{
	init();
	m_parent = parent;
}

GuidedMissile::GuidedMissile(QPoint point, QObject* parent)
	:Bullet(point)
{
	init();
	m_parent = parent;
}

GuidedMissile::~GuidedMissile()
{
	/*qDebug() << "释放了";*/
	if (m_label != nullptr)
	{
		delete m_label;
	}
}

void GuidedMissile::init()
{
	m_temp = getCurrentTime();
	addFrame("导弹发射", "正常");
	setType(Type::weapons);
	ATK = 1000;
}

void GuidedMissile::update()
{
	if (!start)
	{
		AutoMove();
	}
	else
	{
		int num = m_movie->frameCount();
		int n = m_movie->currentFrameNumber();
		if (n + 1 == num)
		{
			m_movie->stop(); m_label->close();
			//qDebug() << "准备释放" ;
			destroy();
		}
	}
		
}

void GuidedMissile::draw(QPainter* painter)
{
	if (!start)
		Bullet::draw(painter);
}


bool GuidedMissile::Collider(const QRect& collider)
{
	if (!start&&Articles::Collider(collider))
	{
		start = true;
		m_movie = Game::res().getMovie("导弹爆炸");
		m_label = new QLabel((QWidget*)m_parent);
		m_label->setMovie(m_movie);
		m_movie->setScaledSize(m_BlastSize);
		int x = (getPos().x() + getSize().width() / 2) - m_BlastSize.width() / 2;
		int y = (getPos().y() + getSize().height() / 2) - m_BlastSize.height() / 2;
		setCollider(getPos()+QPoint(getSize().width() / 2, getSize().height() / 2), m_BlastSize.width() / 2, m_BlastSize.height() / 2);
		//qDebug() << getPos() << getCollider();
		m_label->move(x, y);
		m_movie->setSpeed(50);
		m_movie->start();
		m_label->show();
		return true;
	}
	else if (start && Articles::Collider(collider))
	{
		return true;
	}
	return false;
}
